# -*- coding: utf-8 -*-

# @Time : 2020/7/4 14:57 
# @Author : dzzhyk
# @File : jsonUtil.py 
# @Software: PyCharm

import json


# json工具类


def json2Sql(database, table_name, json_string):
    """
    获取json字符串，返回建表语句
    :param json: json字符串
    :return:
        sql建表语句
    """
    dic = dict(json.loads(json_string))

    sql = "USE `" + database + "`;\n"
    sql += "CREATE TABLE `" + table_name +"`(\n"
    for k, v in dic.items():
        if isinstance(eval(v), str):
            print(k + ": " +"varchar(255)")
        elif isinstance(eval(v), int):
            print(k + ": " + "int")
        elif isinstance(eval(v), bool):
            print(k + ": " + "boolean")
        elif isinstance(eval(v), float):
            print(k + ": " + "double")


    sql += ")ENGINE=InnoDB DEFAULT CHARSET=utf8;\n"
    return sql


if __name__ == '__main__':
    string = '{"crawled": "﻿1593744077", "spider": "car_spider", "href": "https://www.guazi.com/zz/5f8e783bff1c19acx.htm", "title": "郑州奥迪A3 2014款 Sportback 35 TFSI 自动进取型二手车【价格 图片 报价 多少钱】_瓜子二手车", "mileage": "6.41万公里", "emission": "国4(国5)", "gearbox": "自动", "transfers": "3次过户", "region": "zz", "manufacturer": "一汽-大众奥迪", "level": "紧凑型车", "engine": "1.4T/150马力/L4", "gearbox_d": "7挡双离合", "struction": "5门5座两厢车", "LWH": "4319/1785/1441", "wheelbase": "2629", "luggage": "380-1220", "curb_weight": "1340", "displacement": "1.4", "intake_form": "涡轮增压", "cylinders": "4缸", "horsepower": "150", "torque": "250", "fuel": "汽油", "fuel_label": "95号", "fuel_supply": "直喷", "drive_method": "前置前驱", "assistance": "电动助力", "front_suspension": "麦弗逊式独立悬架", "back_suspension": "多连杆独立悬架", "front_brake": "通风盘式", "back_brake": "盘式", "driving_brake": "电子驻车", "front_tire": "205/55 R16", "back_tire": "205/55 R16", "front_airbag": "标配/标配", "side_airbag": "标配/选配", "head_airbag": "标配/-", "tire_pressure": "选配", "locking": "标配", "child_seat": "标配", "keyless": "选配", "abs": "标配", "esp": "标配", "sunroof": "选配", "skylight": "选配", "suction_door": "-", "induction_trunk": "-", "induction_wiper": "选配", "back_wiper": "标配", "windows": "标配/标配", "mirror_elec": "标配", "mirror_heat": "选配", "steering_wheel": "选配", "cruise": "选配", "air_cond": "-", "air_control": "-", "gps": "选配", "radar": "标配", "image": "选配", "leather": "-", "seat_heat": "选配/-"}'
    json2Sql("test", "table_name", string)